<?php
class reportComponents extends sfComponents
{
  public function executeWeeklist()
  {
    // should call this component with para: 'url' => '...', 'month' => '??'
    $this->month = $this->getRequest()->getParameter('month');
    $this->months = Util::getWeeklistForYear(date('Y'));
    $this->today = date('Y-m-d');
  }
  /**
   * provide load distribution chart which could be refreshed by XML
   * 
   * @param
   * 
   * @return unknown_type
   */
  public function executeLoadDistribution()
  {
    $this->day = $this->getRequest()->getParameter('day', null);
    $this->memid = $this->getRequest()->getParameter('memid', null);
    
    $this->members = WrMemberPeer::getMembersByDay($this->day);
    
    /*
    $reports = ReportAnalyzer::getReports($day, $member);
    $this->distribution = ReportAnalyzer::getLoadDistribution($reports);
    if ($this->distribution == null) {
      return sfView::NONE;
    }
    */
    
    $this->chartWidth = 600;
    $this->chartHeight = 400;
    $ran = gettimeofday();
    $this->chartId = 'chart'.$ran['usec'];
    if ($this->day) {
      $this->chartCaption = 'Weekly report from '.Util::getMonday($this->day).' to '.Util::getFriday($this->day);
    } else {
      $this->chartCaption = 'Weekly report from 2009-3-16 to now';
    }
    
    $this->chartUrl = '/report/getLoadDistribution?day='.$this->day.'&memid='.$this->memid;
    //$this->chartItemUrl = '/report/getItems';
    
  }
  
  public function executeUploadForm()
  {

    $request = $this->getRequest();
    $this->form = new UploadReportForm();
    $this->error = '';
    $day = $this->getRequest()->getParameter('day', date('Y-m-d'));
    
    $day = UserData::get('day', date('Y-m-d'));
    $day = Util::getMonday($day);
    //$member_id = $request->getParameter('member');
    $member_id = UserData::get('memid', 1);
    $this->day = $day;
    $this->memid = $member_id;
    
    if ($this->form->populateReportName($day, $member_id) == false) {
      
      return sfView::NONE;
    }
    $this->form->getWidget('report_day')->setDefault($day);
    $this->form->getWidget('report_name')->setDefault($member_id);
    $this->caption = "Upload weekly report for ${day}";
    
    if ($this->getRequest()->isMethod('post')) {
      $this->form->bind($request->getParameter('report'), $request->getFiles('report'));
      if ($this->form->isValid()) {
        $file = $this->form->getValue('report_file');
        $f = new UploadReportForm();

        $names = $this->form->getWidget('report_name')->getChoices();
        $member_id = $this->form->getValue('report_name');
        $name = $names[$member_id];

        $date = $this->form->getValue('report_day');
        
        $uploadDir = sfConfig::get('app_weekly_report_folder').'/';
        $uploadFile = $uploadDir.$file->getOriginalName();

        $file->save($uploadFile);
        
        try {
          $report = new WeeklyReport($uploadFile);
        } catch (Exception $e) {
          unlink($uploadFile);
          $this->error = $e->getMessage();
          return sfView::SUCCESS;
        }

        try {
          $report->save($name, $member_id, $date);
        } catch (Exception $e) {
          unlink($uploadFile);
          $this->error = $e->getMessage();
          return sfView::SUCCESS;
        }
        
        // everything works fine now, rename the file to the standard name
        $newName = $report->getNormName();
        rename($uploadFile, $uploadDir.$newName);
        $response = $this->getResponse();
        UserData::store('memid', $member_id);
        UserData::store('day', $date);
        //$this->forward('report', 'index?name='.$name, '&day='.$date);
      }
    } else {
      return sfView::SUCCESS;
    }    
  }
}
